#include "Drv_AD9226.h"



void AD9226_exTransData(uint16_t *pSrc, float *pDes, uint16_t num)
{
  uint32_t i;
  
  for(i = 0; i < num; ++i)
  {
    pDes[i] = (2048 - pSrc[i]) * 5.0 / 2048;
  }
}



//#define USING_STM32_DRV
#ifdef USING_STM32_DRV
#if 1
#include "BitBand.h"
#define RD_AD9226_D0()  PEin(6)
#define RD_AD9226_D1()  PFin(0)
#define RD_AD9226_D2()  PFin(1)
#define RD_AD9226_D3()  PFin(2)
#define RD_AD9226_D4()  PFin(3)
#define RD_AD9226_D5()  PFin(4)
#define RD_AD9226_D6()  PFin(5)
#define RD_AD9226_D7()  PFin(6)
#define RD_AD9226_D8()  PFin(7)
#define RD_AD9226_D9()  PFin(8)
#define RD_AD9226_D10() PCin(0)
#define RD_AD9226_D11() PCin(1)
#else
#define RD_AD9226_D0()  HAL_GPIO_ReadPin(AD9226_D0_GPIO_Port,  AD9226_D0_Pin )
#define RD_AD9226_D1()  HAL_GPIO_ReadPin(AD9226_D1_GPIO_Port,  AD9226_D1_Pin )
#define RD_AD9226_D2()  HAL_GPIO_ReadPin(AD9226_D2_GPIO_Port,  AD9226_D2_Pin )
#define RD_AD9226_D3()  HAL_GPIO_ReadPin(AD9226_D3_GPIO_Port,  AD9226_D3_Pin )
#define RD_AD9226_D4()  HAL_GPIO_ReadPin(AD9226_D4_GPIO_Port,  AD9226_D4_Pin )
#define RD_AD9226_D5()  HAL_GPIO_ReadPin(AD9226_D5_GPIO_Port,  AD9226_D5_Pin )
#define RD_AD9226_D6()  HAL_GPIO_ReadPin(AD9226_D6_GPIO_Port,  AD9226_D6_Pin )
#define RD_AD9226_D7()  HAL_GPIO_ReadPin(AD9226_D7_GPIO_Port,  AD9226_D7_Pin )
#define RD_AD9226_D8()  HAL_GPIO_ReadPin(AD9226_D8_GPIO_Port,  AD9226_D8_Pin )
#define RD_AD9226_D9()  HAL_GPIO_ReadPin(AD9226_D9_GPIO_Port,  AD9226_D9_Pin )
#define RD_AD9226_D10() HAL_GPIO_ReadPin(AD9226_D10_GPIO_Port, AD9226_D10_Pin)
#define RD_AD9226_D11() HAL_GPIO_ReadPin(AD9226_D11_GPIO_Port, AD9226_D11_Pin)
#endif


uint16_t cnt_sampling;
uint16_t cnt_target;
uint16_t buf_AD_Data[2000];
float real_AD_Data[2000];
uint8_t  cntEndFlag;


extern TIM_HandleTypeDef htim8;


uint16_t AD9226_readData()
{
  uint16_t tmp;

  tmp = (RD_AD9226_D0()  << 11)  +
        (RD_AD9226_D1()  << 10)  +
        (RD_AD9226_D2()  << 9 )  +
        (RD_AD9226_D3()  << 8 )  +
        (RD_AD9226_D4()  << 7 )  +
        (RD_AD9226_D5()  << 6 )  +
        (RD_AD9226_D6()  << 5 )  +
        (RD_AD9226_D7()  << 4 )  +
        (RD_AD9226_D8()  << 3 )  +
        (RD_AD9226_D9()  << 2 )  +
        (RD_AD9226_D10() << 1 )  +
        (RD_AD9226_D11() << 0 );
  
  return (uint16_t)tmp;
}

void AD9226_getDataStream(uint16_t num)
{
  cnt_sampling = 0;
  cnt_target = num + VALID_DATA_SHIFT;
  cntEndFlag = 0;
  HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_1);
  while(!cntEndFlag);
}

void AD9226_transData(uint16_t num)
{
  uint32_t i;
  
  for(i = 0; i < num; ++i)
  {
    real_AD_Data[i] = (2048 - buf_AD_Data[i + VALID_DATA_SHIFT]) * 5.0 / 2048;
  }
}
 


/*******************************************************************
 * driver timer setting
 * 
 * PWM mode 2
 * CH Polarity High
 * 
 * __HAL_TIM_CLEAR_FLAG(&htim8,TIM_SR_UIF);
 * __HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
 * 
 * IRQ sever
 *
 * void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
 * {
 *   if(htim == &htim8)
 *   {
 *     if(cnt_sampling >= cnt_target)
 *     {
 *       HAL_TIM_PWM_Stop_IT(&htim8, TIM_CHANNEL_1);
 *       cntEndFlag = 1;
 *     }
 *     else
 *     {
 *       buf_AD_Data[cnt_sampling] = AD9226_readData();
 *     }
 *     cnt_sampling++;
 *   }
 * }
 * 
 ******************************************************************/
 #endif